package com.jinkaimen.goodcash.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.support.v4.app.NotificationCompat;

import java.util.UUID;

/**
 */
public class NotificationUtils {


    private static final String NOTIFICATION_MSG_SOUND = "notification_msg_sound";
    private static final String NOTIFICATION_MSG_VARIB = "notification_msg_varib";
    private static NotificationManager mManager;
    private static AudioManager mAudioManager;

    public static void show(Context context, int icon,
                            String title, String text, Intent intent, boolean hasSound) {
        if (mManager == null) {
            mManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        if (null != title) {
            builder.setContentTitle(title);
        }

        if (null != text) {
            builder.setContentText(text);
        }

        builder.setSmallIcon(icon).setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis()).setAutoCancel(true);

        int requestCode = UUID.randomUUID().hashCode();
        PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        Notification n = builder.build();
        setSoundAndVarib(context, n, hasSound);
        mManager.notify(requestCode, n);
    }

    public static void setSoundAndVarib(Context context, Notification notification, boolean hasSound) {
        if (mAudioManager == null) {
            mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        }
        switch (mAudioManager.getRingerMode()) {//获取系统设置的铃声模式
            case AudioManager.RINGER_MODE_SILENT://静音模式，值为0，这时候不震动，不响铃
                notification.sound = null;
                notification.vibrate = null;
                break;
            case AudioManager.RINGER_MODE_VIBRATE://震动模式，值为1，这时候震动，不响铃
                notification.sound = null;
                notification.defaults |= Notification.DEFAULT_VIBRATE;
                break;
            case AudioManager.RINGER_MODE_NORMAL://常规模式，值为2，分两种情况：1_响铃但不震动，2_响铃+震动
                boolean soundSetting = hasSound;
                boolean vibrateSetting = soundSetting;

                if (soundSetting && vibrateSetting) {//响铃并震动
                    notification.defaults |= Notification.DEFAULT_VIBRATE;
                    notification.defaults |= Notification.DEFAULT_SOUND;
                } else if(!soundSetting && !vibrateSetting){
                    notification.defaults = Notification.DEFAULT_LIGHTS;
                }else {
                    if (soundSetting) {//无值，为空，不播放铃声
                        notification.defaults = Notification.DEFAULT_SOUND;
                    }

                    if (vibrateSetting){
                        notification.defaults = Notification.DEFAULT_VIBRATE;
                    }
                }
                break;
            default:
                break;
        }
    }

}
